#Keyboard movement, up is up and down is down

import sys
import pygame

pygame.init()
SCREEN_WIDTH  = 600
SCREEN_HEIGHT = 400

hwinfo = pygame.display.Info()
Clock = pygame.time.Clock()

initstatus                = pygame.DOUBLEBUF
initstatus               |= pygame.NOFRAME
if hwinfo.hw: initstatus |= pygame.HWSURFACE
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), initstatus)

class player:
   id, x, y = 0, 0, 0
   def __init__(self, tid, tx, ty):
      self.id, self.x, self.y = tid, tx, ty
   
   def draw(self, screen):
      pygame.draw.circle(screen, (120,0,0), (self.x, SCREEN_HEIGHT-self.y), 10, 0)
   def inc(self, v):
      if   v == "x+": self.x += 1
      elif v == "x-": self.x -= 1
      elif v == "y+": self.y += 1
      elif v == "y-": self.y -= 1

ents        = [player("p1", 50, 50)]
keys        = [pygame.K_d, pygame.K_a, pygame.K_w, pygame.K_s]
keys_down   = {keys[0] : False, keys[1] : False, keys[2] : False, keys[3] : False}
keys_action = {keys[0] : lambda: ent.inc("x+"),
               keys[1] : lambda: ent.inc("x-"),
               keys[2] : lambda: ent.inc("y+"),
               keys[3] : lambda: ent.inc("y-")}
play = True
while play:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         play = 0
         break
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_ESCAPE:
            play = 0
            break
         for key in keys:
            if event.key == key:
               keys_down [key] = True
               break
      elif event.type == pygame.KEYUP:
         for key in keys:
            if event.key == key:
               keys_down [key] = False
               break
   for key in keys:
      if keys_down[key]:
         keys_action[key]()
   for ent in ents:
      ent.draw(screen)
   pygame.display.flip()
   screen.fill((0,0,0))
   Clock.tick(60)
   
